import {
  listPermission,
  listAllPermission,
  createPermission,
  updatePermission,
  deletePermission
} from "@/pages/Setting/services/permission";

export default {
  namespace: "permission",

  state: {
    data: {
      list: [],
      pagination: {}
    }
  },

  effects: {
    *list({ payload }, { call, put }) {
      const response = yield call(listPermission, payload);
      yield put({
        type: "save",
        payload: response
      });
    },
    *listAll({ payload }, { call, put }) {
      const response = yield call(listAllPermission, payload);
      yield put({
        type: "savePermTreeData",
        payload: response
      });
    },
    *create({ payload, callback }, { call, put }) {
      const response = yield call(createPermission, payload);
      yield put({
        type: "save",
        payload: response
      });
      if (callback) callback();
    },
    *update({ payload, callback }, { call, put }) {
      const response = yield call(updatePermission, payload);
      yield put({
        type: "save",
        payload: response
      });
      if (callback) callback();
    },
    *delete({ payload, callback }, { call, put }) {
      const response = yield call(deletePermission, payload);
      yield put({
        type: "save",
        payload: response
      });
      if (callback) callback();
    }
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data
      };
    },
    savePermTreeData(state, action) {
      // console.log(action.payload.data);
      return {
        ...state,
        permTreeData: action.payload.data
      };
    }
  }
};
